﻿var RoleData = [{ id: 0, text: '普通用户' }, { id: 1, text: '管理员'}];
var GenderData = [{ id: 1, text: '男' }, { id: 2, text: '女'}];
mini.parse();
var grid = mini.get("grid1");
grid.setUrl("../service/SystemManageHandler.ashx?action=GetUserList");
grid.load();
grid.on("drawcell", function(e) {
    var record = e.record,
        column = e.column,
        field = e.field,
        value = e.value;
    //将性别文本替换成图片
    if (column.field == "Gender") {
        if (e.value == 1) {
            e.cellHtml = "<span class='icon-boy'></span>"
        } else {
            e.cellHtml = "<span class='icon-female'></span>"
        }
    }
});

function add() {
    mini.open({
        url: "SystemManage/AddUserInfoForm.aspx",
        title: "新增用户", width: 440, height: 330,
        onload: function() {
            var iframe = this.getIFrameEl();
            var data = { action: "new" };
            iframe.contentWindow.SetData(data);
        },
        ondestroy: function(action) {
            grid.reload();
        }
    });
}
function edit() {
    var row = grid.getSelected();
    if (row) {
        mini.open({
            url: "SystemManage/EditUserInfoForm.aspx",
            title: "编辑用户", width: 440, height: 305,
            onload: function() {
                var iframe = this.getIFrameEl();
                var data = { action: "edit", UserId: row.UserId };
                iframe.contentWindow.SetData(data);

            },
            ondestroy: function(action) {
                grid.reload();
            }
        });

    } else {
        mini.alert("请选中一条记录!");
    }
}
function remove() {
    var row = grid.getSelected();
    if (row) {
        mini.confirm("您确定要删除:" + row.UserName + "？", "确定？",
                    function(action) {
                        if (action == "ok") {
                            $.ajax({
                                url: "../Service/SystemManageHandler.ashx?action=RemoveUser&UserId=" + row.UserId,
                                success: function(text) {
                                    grid.reload();
                                },
                                error: function(jqXHR, textStatus, errorThrown) {
                                    alert(jqXHR.responseText);
                                }
                            });
                        } else {
                            return false;
                        }
                    });

    } else {
        mini.alert("请选中一条记录!");
    }
}
function modifypwd() {
    var row = grid.getSelected();
    if (row) {
        mini.open({
            url: "SystemManage/ModifyPwdInfoForm.aspx",
            title: "修改密码", width: 300, height: 185,
            onload: function() {
                var iframe = this.getIFrameEl();
                var data = { action: "modify", UserName: row.UserName, FullName: row.FullName };
                iframe.contentWindow.SetData(data);
            },
            ondestroy: function(action) {
                grid.reload();
            }
        });

    } else {
        mini.alert("请选中一条记录!");
    }
}
function search(e) {
    var UserName = mini.get("UserName").getValue();
    var FullName = mini.get("FullName").getValue();
    var Role = mini.get("Role").getValue();
    var Gender = mini.get("Gender").getValue();
    grid.load({ UserName: UserName, FullName: FullName, Role: Role, Gender: Gender });
}
function onKeyEnter(e) {
    search();
}
function reset(e) {
    mini.get("UserName").setValue("");
    mini.get("FullName").setValue("");
    mini.get("Role").setValue("");
    mini.get("Gender").setValue("");
}
function onRoleRenderer(e) {
    if (e.value == "0") { return "<span \">普通用户</span>"; }
    else if (e.value == "1") { return "<span \">管理员</span>"; }
    else { return ""; }
}
function CloseWindow(action) {
    if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
    else window.close();
}
function close() {
    CloseWindow("close");
}